package reading.util.utils;

import java.util.regex.Pattern;

/**
 * @author TXW
 * @version V1.0
 * @Title: ${file_name}
 * @date 2018/11/16 10:52
 */
public class PatternUtil {
    /**
     * 正则表达式：验证汉字
     */
    public static final String CHINESE = "^[\u4e00-\u9fa5],{0,}$";
    /**
     * 正则表达式：验证URL
     */
    public static final String URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
    /**
     * 正则表达式：验证IP地址
     */
    public static final String IP_ADDRESS = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";
    /**
     * 验证邮箱
     */
    public static final String EMAIL_STR = "^[A-Za-z\\d]+([-_.][A-Za-z\\d]+)*@([A-Za-z\\d]+[-.])+[A-Za-z\\d]{2,4}$";
    /**
     * 验证手机号
     */
    public static final String PHONE_STR = "^1[345789]\\d{9}$";
    public static final String ID_CARD_STR = "(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)";
    public static final String QQ = "[1-9][0-9]{4,14}";
    private static final Pattern REGEX_CHINESE = Pattern.compile(CHINESE);
    private static final Pattern REGEX_URL = Pattern.compile(URL);
    private static final Pattern REGEX_IP_ADDR = Pattern.compile(IP_ADDRESS);
    private static final Pattern EMAIL = Pattern.compile(EMAIL_STR);
    private static final Pattern PHONE = Pattern.compile(PHONE_STR);
    private static final Pattern ID_CARD = Pattern.compile(ID_CARD_STR);

    private static boolean judge(String str, Pattern pattern) {
        return pattern.matcher(str).matches();
    }

    public static boolean isPhone(String str) {
        return judge(str, PHONE);
    }

    public static boolean isEmail(String str) {
        return judge(str, EMAIL);
    }

    public static boolean isIdCard(String str) {
        return judge(str, ID_CARD);
    }

}
